package com.cai.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.cai.dao.OrderSettingDao;
import com.cai.pojo.OrderSetting;
import com.cai.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    public void add(List<OrderSetting> orderSettingList) {
        if(orderSettingList!=null&&orderSettingList.size()>0){
            for (OrderSetting orderSetting : orderSettingList) {
                long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if(count>0){
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                }else {
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    //根据月份获取预约数据
    public List<Map> getOrderSettingByMonth(String date) {
        String dateBegin=date+"-1";
        String dateEnd=date+"-31";
        Map map=new HashMap();
        map.put("dateBegin",dateBegin);
        map.put("dateEnd",dateEnd);
        List<OrderSetting>list = orderSettingDao.getOrderSettingByMonth(map);
        List<Map>result=new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            Map orderSettingMap=new HashMap();
            orderSettingMap.put("date",orderSetting.getOrderDate().getDate());//获取日期
            orderSettingMap.put("number",orderSetting.getNumber());//设置可预约人数
            orderSettingMap.put("reservations",orderSetting.getReservations());//已预约人数
            result.add(orderSettingMap);
        }
        return result;
    }

   //修改预约人数
    public void editNumberByDate(OrderSetting orderSetting) {
        //先根据日期查询该日期是否设置了预约人数
        long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if(count>0){
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else {
            orderSettingDao.add(orderSetting);
        }
    }
}
